package com.aaa.service.impl;

import com.aaa.dao.ActivityMapper;
import com.aaa.entity.Activity;
import com.aaa.mq.ActivityMqConfig;
import com.aaa.service.ActivityServiceDao;
import com.aaa.util.ActivityUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ActivityServiceimpl extends ServiceImpl<ActivityMapper,Activity> implements ActivityServiceDao {
    @Resource
    ActivityMapper acivityDao;

    @Resource
    ActivityUtil activityUtil;

    @Override
    public Integer Addactivity(Activity activity) {
        activity.setVersion(1);
        int i = acivityDao.insert(activity);
        System.out.println(activity.getAid());
        if (i==1){
            //设置自动倒计时间
            activityUtil.sendMsg(ActivityMqConfig.ACTIVITY_EX, ActivityMqConfig.ACTIVITY_KEY_START, activity, activity.getStart());
        }
        return i;
    }

    @Override
    public List<Activity> find() {
        return acivityDao.find();
    }
    @Override
    public Integer updactivity(Activity newactivity) {
        int i;
        Activity oldactivity = acivityDao.selectById(newactivity.getAid());
      if (oldactivity.getStart().equals(newactivity.getStart())){
         i= acivityDao.updateById(newactivity);
      }else {
        newactivity.setVersion(oldactivity.getVersion()+1);
       i= acivityDao.updateById(newactivity);
          activityUtil.sendMsg(ActivityMqConfig.ACTIVITY_EX, ActivityMqConfig.ACTIVITY_KEY_START, newactivity, newactivity.getStart());
      }
   return i;
    }
}
